package vn.demand.dragon.view
{
	import flexed.widgets.alerts.alert;
	
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import vn.demand.dragon.CommonCommand;
	import vn.demand.dragon.FFIAApplicationFacade;
	import vn.demand.dragon.view.components.ProjectPanel;

	public class ProjectPanelMediator extends Mediator
	{
		public static const NAME:String = "ProjectPanelMediator";
		
		private var projectVO:Object;
		
		public function ProjectPanelMediator(projectVO:Object, mediatorName:String=NAME, viewComponent:Object=null)
		{
			super(mediatorName, viewComponent);
			
			panel.viewId = this.getMediatorName();
			
			this.projectVO = projectVO;
			panel.label = "" + projectVO.project_iso_code;
		}
		
		override public function listNotificationInterests():Array 
        {
            return [ 
            		FFIAApplicationFacade.CLOSE_PROJECT_PANEL,
            		CommonCommand.CLOSE_TAB,
            		
					 ];
        }
        
        override public function handleNotification( note:INotification ):void 
        {
        	switch(note.getName()) {
        		case FFIAApplicationFacade.CLOSE_PROJECT_PANEL:
        		case CommonCommand.CLOSE_TAB:
        			var viewId:String = note.getType() as String;
        			if (note.getBody() == panel || panel.viewId == viewId) {
        				doClosePanel();
        			}
        			return;
        	}
        }
        
        private function doClosePanel() {
        	if (panel.projectItemScreen.enableSave && panel.projectItemScreen.enableSave) {
        		alert.confirm("Do you want to save?", handleConfirmSave, Alert.CANCEL);
			} else {
				sendNotification(FFIAApplicationFacade.CLOSE_FINISH_PROJECT_PANEL, projectVO);
			}
//        			if (panel.taskScreen.enableSave && panel.taskScreen.enableSave) {
//        				alert.confirm("Do you want to save?", handleConfirmSave, Alert.CANCEL);
//        			} else if (panel.taskScreen.enableSave) {
//        				alert.confirm("Do you want to save?", handleConfirmSave, Alert.CANCEL);
//        			} else {
//        				sendNotification(FFIAApplicationFacade.CLOSE_FINISH_PROJECT_PANEL, projectVO);
//        			}
        }
        
        private function handleConfirmSave(event:CloseEvent) {
        	if (event.detail == Alert.CANCEL) {
        		return;
        	}
        	if (event.detail == Alert.NO) {
        		sendNotification(FFIAApplicationFacade.CLOSE_FINISH_PROJECT_PANEL, projectVO);
        		return;
        	}
        	var canClose:Boolean = true;
//        	var m:TaskScreenMediator = facade.retrieveMediator(TaskScreenMediator.makeId(projectVO)) as TaskScreenMediator;
//        	if (m.taskScreen.enableSave) {
//	        	if (!m.doSaveTask()) {
//	        		canClose = false;
//	        	}
//        	}
        	if (canClose) {
        		sendNotification(FFIAApplicationFacade.CLOSE_FINISH_PROJECT_PANEL, projectVO);        		
        	}
        } 
		
		public function get panel():ProjectPanel {
			return viewComponent as ProjectPanel;
		}
		
		public static function makeId(projectVO:Object):String {
			return ProjectPanelMediator.NAME + "::" + projectVO.project_id;
		}
		
	}
}